from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from app import db


class BaseModel(db.Model):
    __abstract__ = True
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    remark = db.Column(db.Text, comment='备注')


class User(BaseModel):
    __tablename__ = 'user'

    STATUS = (
        (0, 'no'),
        (1, 'yes')
    )

    id = db.Column(db.Integer, primary_key=True, autoincrement=True,comment='主键')
    name = db.Column(db.String(255), unique=True, nullable=False, comment='名字')
    nickname = db.Column(db.String(255), nullable=True, comment='昵称')
    _password_hash = db.Column(db.String(255), nullable=True)
    email = db.Column(db.String(255), comment='邮件')
    is_active = db.Column(db.SmallInteger, default=0, comment='默认未激活')
    wechar_id = db.Column(db.Integer, comment='微信id')

    def _to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'email': self.email,
            'is_active': self.STATUS[self.is_active][1]
        }

    def __str__(self):
        return self.name
